package com.tiger.cloud.service.visitor;

import com.tiger.cloud.common.exception.ServiceException;
import com.tiger.cloud.enums.UserStatusEnum;
import com.tiger.cloud.model.crm.User;
import com.tiger.cloud.model.ext.UserInfos;
import com.tiger.cloud.model.ext.VisitorPojo;
import com.tiger.cloud.model.visitor.Visitor;
import com.tiger.cloud.dao.visitor.VisitorDao;
import com.tiger.cloud.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tiger.cloud.common.mybatis.TigerBaseService;
import com.tiger.cloud.common.mybatis.MybatisTigerDao;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * visit·Ã¿ÍÍ³¼Æ
 */
@Service("visitorService")
public class VisitorService extends TigerBaseService<Visitor> implements IVisitorService{
    
    @Autowired
	private VisitorDao visitorDao;

	@Override
	public MybatisTigerDao<Visitor> getEntityDao() {
		return visitorDao;
	}

	@Transactional(propagation= Propagation.REQUIRED)
	public boolean insertVisitor(Visitor visitor) throws Exception {
		try {
			Visitor visit = visitorDao.findVisitAndUserId(visitor);
			if(visit == null){
				return visitorDao.insert(visitor) > 0 ? true :false;
			}else{
               return visitorDao.update(visit)> 0 ? true :false;
			}
		} catch (Exception e) {
			throw new ServiceException("添加访客失败："+e.getMessage().toLowerCase());
		}
	}

	@Override
	public Page<VisitorPojo> findVisitList(VisitorPojo visit) {
		int limitStart=visit.getLimitStart(),limitEnd = visit.getLimitEnd();
		Visitor visitor = new Visitor();
		visitor.setUserId(visit.getUserId());
		long count = visitorDao.countByEntity(visitor);
		Page<VisitorPojo> page = new Page<VisitorPojo>((int)(limitEnd/(limitEnd-limitStart)), (limitEnd-limitStart));
		page.setData(visitorDao.findVisitList(visit));
		page.setTotalCount(count);
		return page;
	}

}